package com.xudow.app.dynamicstate_old.module.message;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.widget.SwipeRefreshLayout;

import com.jude.beam.expansion.BeamBasePresenter;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.jude.utils.JUtils;
import com.xudow.app.dynamicstate_old.data.DynamicModel;
import com.xudow.app.dynamicstate_old.data.server.ErrorTransform;
import com.xudow.app.dynamicstate_old.domain.entity.PushMessage;

import java.util.ArrayList;
import java.util.List;

import rx.Subscriber;

/**
 * Created by ske on 2016/7/6.
 */
public class SystemMsgPresenter extends BeamBasePresenter<SystemMsgActivity> implements SwipeRefreshLayout.OnRefreshListener, RecyclerArrayAdapter.OnLoadMoreListener {

    long lastTime = 0;

    @Override
    protected void onCreate(@NonNull SystemMsgActivity view, Bundle savedState) {
        super.onCreate(view, savedState);
        onRefresh();
    }

    int page;

    @Override
    public void onLoadMore() {
        DynamicModel.getInstance()
                .getSystemMsgs(lastTime, 2, 10)
                .compose(new ErrorTransform<>(ErrorTransform.ServerErrorHandler.TOAST))
                .doOnNext(list -> {
                    if (list != null && list.size() > 0)
                        lastTime = list.get(list.size() - 1).getSendTime();
                })
                .subscribe(new Subscriber<List<PushMessage>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        if (getView().getAdapter().getCount() == 0) {
                            //没有数据的时候出错，就显示全屏的错误提示
                            getView().recyclerView.showError();
                        } else {
                            //有数据时的出错，在最后一条显示错误提示
                            getView().getAdapter().pauseMore();
                        }
                    }

                    @Override
                    public void onNext(List<PushMessage> messageList) {
                        if (messageList == null)
                            messageList = new ArrayList<>();
                        page++;
                        getView().getAdapter().addAll(messageList);
                    }
                });
    }

    @Override
    public void onRefresh() {
        DynamicModel.getInstance()
                .getSystemMsgs(lastTime, 1, 10)
                .compose(new ErrorTransform<>(ErrorTransform.ServerErrorHandler.TOAST))
                .doOnNext(list -> {
                    if (list != null && list.size() > 0)
                        lastTime = list.get(list.size() - 1).getSendTime();
                })
                .subscribe(new Subscriber<List<PushMessage>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        getView().stopRefresh();
                        getView().recyclerView.showError();
                    }

                    @Override
                    public void onNext(List<PushMessage> messageList) {
                        if (messageList == null)
                            messageList = new ArrayList<PushMessage>();
                        page = 1;
                        getView().getAdapter().clear();
                        getView().getAdapter().addAll(messageList);
                    }
                });
    }

    public void delMsg(int position, long pid) {
        DynamicModel.getInstance().delMsg(pid)
                .subscribe(o -> {
                    JUtils.Toast("删除成功");
                    getView().getAdapter().remove(position);
//                    getView().getAdapter().notifyItemRemoved(position);
                });
    }

    public void clearMsg() {
        DynamicModel.getInstance().clearSystemMsg()
                .subscribe(message -> {
                    JUtils.Toast("清除成功");
                    onRefresh();
                });
    }
}
